package com.fau.hm.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.hibernate.Hibernate;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

/**
 * @desc 员工档案信息
 * @author dangjh
 * @datetime 2014年10月28日,下午8:33:18
 */
@Entity
@Table(name = "medical_archival_info")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class MedicalArchivalInfo implements Serializable, JSONAware {

	private static final long serialVersionUID = -5608140955143441671L;

	private String id;
	
	@NotNull(message = "用户id不能为空！")
	private MedicalAdmin user;				// 员工对象
	private String idCardNum;				// 身份证号
	private String gender;					// 性别
	private Date entryTime;					// 入职时间
	private String contractNum;				// 合同编号
	private String archivesUrl;				// 附件地址
	private Date createTime = new Date();
	private Date updateTime = new Date();
	
	private String userId;
	private String name;
	private String genderName;
	private String deptType;
	private String groupIds;
	
	public MedicalArchivalInfo(){
	}
	
	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "id", length = 32)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@OneToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "user_id")
	public MedicalAdmin getUser() {
		return user;
	}

	public void setUser(MedicalAdmin user) {
		this.user = user;
	}

	@Column(name = "id_card_num", length = 25)
	public String getIdCardNum() {
		return idCardNum;
	}

	public void setIdCardNum(String idCardNum) {
		this.idCardNum = idCardNum;
	}

	@Column(name = "gender", length = 20)
	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	@Column(name = "entry_time")
	public Date getEntryTime() {
		return entryTime;
	}

	public void setEntryTime(Date entryTime) {
		this.entryTime = entryTime;
	}

	@Column(name = "contract_num", length = 50)
	public String getContractNum() {
		return contractNum;
	}

	public void setContractNum(String contractNum) {
		this.contractNum = contractNum;
	}

	@Column(name = "archives_url", length = 255)
	public String getArchivesUrl() {
		return archivesUrl;
	}

	public void setArchivesUrl(String archivesUrl) {
		this.archivesUrl = archivesUrl;
	}

	@Column(name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "update_time")
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	@Transient
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
	
	@Transient
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Transient
	public String getGenderName() {
		return genderName;
	}

	public void setGenderName(String genderName) {
		this.genderName = genderName;
	}

	@Transient
	public String getDeptType() {
		return deptType;
	}

	public void setDeptType(String deptType) {
		this.deptType = deptType;
	}
	
	@Transient
	public String getGroupIds() {
		return groupIds;
	}

	public void setGroupIds(String groupIds) {
		this.groupIds = groupIds;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("name", user.getName());
		obj.put("idCardNum", idCardNum);
		obj.put("gender", gender);
		if(null != entryTime){
			obj.put("entryTime", DateFormatUtils.format(entryTime, "yyyy-MM-dd"));
		}
		obj.put("contractNum", contractNum);
		obj.put("archivesUrl", archivesUrl);
		obj.put("createTime", DateFormatUtils.format(createTime, "yyyy-MM-dd"));
		obj.put("updateTime", DateFormatUtils.format(updateTime, "yyyy-MM-dd"));
		obj.put("mobile", user.getMobile());
		obj.put("deptName", user.getDeptName());
		if(Hibernate.isInitialized(user.getGroup())) {
			if(null != user.getGroup()) {
				obj.put("groupName", user.getGroup().getName());
			}
		}
		obj.put("groupIds", groupIds);
		obj.put("genderName", genderName);
		obj.put("deptType", deptType);
		return obj.toJSONString();
	}

}
